package com.runen.infinitemovement.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.ColumnActivityNameAdapter;
import com.runen.infinitemovement.adapter.SportsTypeNameAdapter;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.StatusBarUtil;
import com.runen.infinitemovement.tool.notchtool.NotchTools;
import com.runen.infinitemovement.tool.notchtool.core.NotchProperty;
import com.runen.infinitemovement.tool.notchtool.core.OnNotchCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ConsultFragment extends Fragment {
    @BindView(R.id.rv_title)
    RecyclerView rvTitle;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    private View view;
    private List<String> stringList = new ArrayList<>();
    private SportsTypeNameAdapter sportsTypeNameAdapter;
    private ColumnActivityNameAdapter columnActivityNameAdapter;
    private String[] sportsTypeNames = new String[]{"热门新闻", "长征故事"};

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_consult, container, false);
            ButterKnife.bind(this, view);
            initView();
        }
        return view;
    }

    private void initView() {
        //适配刘海屏
        NotchTools.getFullScreenTools().translucentStatusBar(getActivity(), new OnNotchCallBack() {
            @Override
            public void onNotchPropertyCallback(NotchProperty notchProperty) {
                llContent.setPadding(0, CommonUtil.getStatusBarHeight(getActivity()), 0, 0);
            }
        });
        LinearLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
        rvTitle.setLayoutManager(layoutManager);
        sportsTypeNameAdapter = new SportsTypeNameAdapter(getActivity());
        rvTitle.setAdapter(sportsTypeNameAdapter);
        stringList.clear();
        for (int i = 0; i < sportsTypeNames.length; i++) {
            stringList.add(sportsTypeNames[i]);
        }
        sportsTypeNameAdapter.setNewData(stringList);
        initSportsTypeName();
        sportsTypeNameAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                sportsTypeNameAdapter.setCheckPosition(position);
                sportsTypeNameAdapter.notifyDataSetChanged();
                viewPager.setCurrentItem(position);
            }
        });
    }


    private void initSportsTypeName() {
        viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                if (position == 0) {
                    NewsFragment newsFragment = (NewsFragment) NewsFragment.newInstance(position + 1);
                    return newsFragment;
                } else {
                    CollegeFragment newsFragment = (CollegeFragment) CollegeFragment.newInstance(position + 1);
                    return newsFragment;
                }
            }

            @Override
            public int getCount() {
                return stringList.size();
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                sportsTypeNameAdapter.setCheckPosition(position);
                sportsTypeNameAdapter.notifyDataSetChanged();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
